<?php
/* ******************************
 *	Workers aJax actions
 * ******************************
 */
include('../../includes/classes/core.php');
$action 	= $_REQUEST['act'];
$user_id	= $_SESSION['USERID'];
$error		= '';
$data		= '';

switch ($action) {
	case 'get_add_page':
		$page		= GetPage();
		$data		= array('page'	=> $page);
		
		break;
	case 'get_edit_page':
	    $barcode_id		= $_REQUEST['id'];
		$page		= GetPage(GetBarcode($barcode_id));
        
        $data		= array('page'	=> $page);
        
	    break;    
	case 'get_list':
	   $count	= $_REQUEST['count'];
	    $hidden	= $_REQUEST['hidden'];

		$rResult = mysql_query("SELECT		`barcodes`.`id`,
											`production`.`name`,
									        `client`.`name`,
											`barcodes`.serial_number,
									        `barcodes`.`code`,
									        `barcodes`.`create_date`,
										    `barcodes`.`comment`				
									FROM   `barcodes` 
									LEFT JOIN `client` ON     `client`.`id` = SUBSTR(`barcodes`.`code`, 2, 4)
									LEFT JOIN `production` ON     `production`.`id` = SUBSTR(`barcodes`.`code`, 6, 4)
									WHERE  `barcodes`.`actived` = 1 && NOT ISNULL(`barcodes`.`serial_number`)");
		
		$data = array(
				"aaData"	=> array()
		);
		
		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{

				$row[] = $aRow[$i];
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$data['aaData'][] = $row;
		}

        break;
    case 'save_barcode':
		$barcode_id = $_REQUEST['id'];
		
    	$client 		= $_REQUEST['bcl'];
		$client_id		= GetClientID($client);
		
		$printer	 	= $_REQUEST['bp'];
		$printer_id		= GetPrinterID($printer);
		
		$serial_number 	= $_REQUEST['sn'];
		$comment		= $_REQUEST['com'];
		
		$array		= array();

		if(empty($barcode_id)){
			$local_id = GetLocalID();			
			$barcode = GenerateBarCode($client_id, $printer_id, $local_id);
			$array[0] = AddBarcode($local_id, $user_id, $barcode,$serial_number, $comment);
		}else{
			Disable($barcode_id);
			$local_id = GetLocalID();				
			$barcode = GenerateBarCode($client_id, $printer_id, $local_id);
			$array[0] = SaveBarcode($local_id, $user_id, $barcode_id, $barcode,$serial_number, $comment);
		}
		
		$data = array('barcode_list' => json_encode($array));
		
        break;
    case 'save_barcode1':        
        mysql_query("UPDATE `barcodes` 
            SET `serial_number`='$_REQUEST[sn]', 
                `comment`='$_REQUEST[com]' 
            WHERE (`id`='$_REQUEST[id]')");
        break;
    case 'disable':
		$barcode_id = $_REQUEST['id'];
		Disable($barcode_id);
		
        break;
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	Workers Functions
 * ******************************
 */

function AddBarcode($local_id, $user_id, $barcode, $serial_number, $comment)
{
	$c_date		= date('Y-m-d H:i:s');
	
	mysql_query("INSERT INTO `barcodes`
					(`id`, `user_id`, `code`, `create_date`,`serial_number`, `comment`) 
				 VALUES
					($local_id, $user_id, '$barcode', '$c_date','$serial_number', '$comment')");
	
	return mysql_insert_id();
}

function SaveBarcode($local_id ,$user_id, $barcode_id, $barcode, $serial_number, $comment)
{
	$c_date		= date('Y-m-d H:i:s');
	
	mysql_query("INSERT INTO `barcodes`
					(`id`, `user_id`, `code`,`parent`, `create_date`,`serial_number`, `comment`) 
				 VALUES
					($local_id, $user_id, '$barcode',$barcode_id, '$c_date',$serial_number, '$comment')");
	
	return mysql_insert_id();
}

function Disable($barcode_id)
{
    mysql_query("UPDATE `barcodes`
				 SET    `actived` = 0
				 WHERE  `id` = $barcode_id");
}

function GetClientID($client_name)
{
	$client_name = htmlspecialchars($client_name, ENT_QUOTES);
	$res = mysql_fetch_assoc(mysql_query("	SELECT	`id`
											FROM	`client`
											WHERE	`name` = '$client_name'"));
	return $res['id'];
}

function GetPrinterID($printer_name)
{
	$cartridge_name = htmlspecialchars($cartridge_name, ENT_QUOTES);
	$res = mysql_fetch_assoc(mysql_query("	SELECT	`id`
											FROM	`production`
											WHERE	`name` = '$printer_name'"));
	return $res['id'];
}

function GetLocalID()
{
	GLOBAL $db;
	$id = $db->increment('barcodes');
	return $id;
}

function GenerateBarCode($client_id, $printer_id, $barcode_id){
	switch( strlen($client_id) ){
		case 1 :  $client_id = '000' . $client_id;
			break;
		case 2 :  $client_id = '00' . $client_id;
			break;
		case 3 :  $client_id = '0' . $client_id;
			break;
		case 4 :  $client_id = '' . $client_id;
			break;
	}
	switch( strlen($printer_id) ){
		case 1 :  $printer_id = '000' . $printer_id;
			break;
		case 2 :  $printer_id = '00' . $printer_id;
			break;
		case 3 :  $printer_id = '0' . $printer_id;
			break;
	}
	switch( strlen($barcode_id) ){
		case 1 :  $barcode_id = '00000' . $barcode_id;
			break;
		case 2 :  $barcode_id = '0000' . $barcode_id;
			break;
		case 3 :  $barcode_id = '000' . $barcode_id;
			break;
		case 4 :  $barcode_id = '00' . $barcode_id;
			break;
		case 5 :  $barcode_id = '0' . $barcode_id;
			break;
	}
	return '3' . $client_id . $printer_id . $barcode_id;
}

function GetBarcode($barcode_id) 
{
    $res = mysql_fetch_assoc(mysql_query("	SELECT	`barcodes`.`id`,
    												`barcodes`.`serial_number` as `serial_number`,
    												`client`.`name` AS `client`,
    												`production`.`name` AS `printer`,
    												`barcodes`.`comment` as `comment`
											FROM	`barcodes` 
											LEFT JOIN `client` ON `client`.`id` = SUBSTR(`barcodes`.`code`, 2, 4)
											LEFT JOIN production ON production.id = SUBSTR(`barcodes`.`code`, 6, 4)
											WHERE   `barcodes`.`id` = $barcode_id"));
	return $res;
}

function GetPage($res = '')
{
	$data = '
	<div id="dialog-form">
	    <fieldset>
	    	<legend>ძირითადი ინფორმაცია</legend>
	    	
	    	<table class="dialog-form-table">
				<tr>
					<td style="width: 170px;"><label for="barcode_client">კლიენტი</label></td>
					<td>
						<div class="seoy-row" id="barcode_client_seoy">
							<input type="text" id="barcode_client" class="idle seoy-large" onblur="this.className=\'idle seoy-large\'" onfocus="this.className=\'activeField seoy-large\'" value="' . $res['client'] . '" />
							<button id="client_btn" class="combobox">barcode_client</button>
						</div>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="barcode_printer">პრინტერი</label></td>
					<td>
						<div class="seoy-row" id="barcode_printer_seoy">
							<input type="text" id="barcode_printer" class="idle seoy-large" onblur="this.className=\'idle seoy-large\'" onfocus="this.className=\'activeField seoy-large\'" value="' . $res['printer'] . '" />
							<button id="barcode_printer_btn" class="combobox">barcode_printer</button>
						</div>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="serial_number">სერიული ნომერი</label></td>
					<td>
							<input type="text" id="serial_number" class="idle seoy-large" onblur="this.className=\'idle seoy-large\'" onfocus="this.className=\'activeField seoy-large\'" value="' . $res['serial_number'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="comment">შენიშვნა</label></td>									
					<td valign="top">
						<textarea id="comment" class="idle large" cols="40" rows="3">' . $res['comment'] . '</textarea>
					</td>
				</tr>									
			</table>
			<!-- ID -->
			<input type="hidden" id="barcode_id" value="' . $res['id'] . '" />
        </fieldset>
    </div>
    ';
	return $data;
}

?>